import {Injectable} from '@angular/core';
import Common from "../common/common";
import {Http, RequestOptions, Headers} from "@angular/http";

@Injectable()
export class ExtendHttpService {

  private headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'});
  private options = new RequestOptions({headers: this.headers, withCredentials: true});

  constructor(private http: Http) {
  }

  toUrl(obj) {
    let str = '';
    for (let i in obj) {
      str += '&' + i + '=' + obj[i];
    }
    str = str.substr(1);
    return str;
  }

  post(url, params) {
    return this.http.post(Common.domain + url, this.toUrl(params), this.options);
  }

  get(url, params) {
    let p = '';
    if (params) {
      p = '?' + this.toUrl(params);
    }
    return this.http.get(Common.domain + url + p, this.options);
  }
}
